﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web.Security;
using WF.BusinessObject;
using WF.DAL;
using System.Data;
using System.Web;

namespace WF.Common
{
    public class SessionManager : BOBase
    {
        #region 构造函数
        public SessionManager()
            : base()
        { }

        public SessionManager(WFDBDataContext DataContext)
            : base(DataContext)
        { }

        #endregion

        /// <summary>
        /// 通过集成身份验证取得K3用户信息,自动登入
        /// </summary>
        public UserProfile CurrentUser_Bak
        {
            get
            {
                bool IsError = false;
                UserProfile userprofile = new UserProfile();
                Com.Object.Fields fd = new Com.Object.Fields();
                AD.AD ad = new AD.AD();
                string identityName = System.Web.HttpContext.Current.User.Identity.Name;
                //if(string.IsNullOrEmpty(identityName))
                //{
                //    identityName = "train8";
                //}
                if ((null == System.Web.HttpContext.Current) || (null == System.Web.HttpContext.Current.Session) || (null == System.Web.HttpContext.Current.Session[SessionKeys.CurrentUser]))
                {

                    SHZYK3.K3 probj = new SHZYK3.K3();

                    DataSet ds = null;
                    try
                    {
                        ds = probj.GetEmpInfoByDomainAccount(identityName);
                        fd.LoadDataFromDataSet(ds, true);
                        //string loginName = "CENTALINE\\"+fd.GetFieldValue("Email").ToString().Split('@')[0];
                        string loginName = fd.GetFieldValue("Email").ToString().Split('@')[0];
                        userprofile.LoginName = loginName; // fd.GetFieldValue("Email");
                        userprofile.Empname = fd.GetFieldValue("EmpName");
                        userprofile.DeptID = fd.GetFieldValue("DeptID");
                        userprofile.EmpID = fd.GetFieldValue("EmpID");
                        //userprofile.
                    }
                    catch
                    {
                        IsError = true;
                    }
                    if (null == ds || IsError == true)
                    {
                        try
                        {
                            ds = ad.ListADUserProperties("", identityName);
                            fd.LoadDataFromDataSet(ds, true);
                            userprofile.LoginName = identityName;  //must
                            userprofile.Empname = fd.GetFieldValue("cn");    //must
                            userprofile.DeptID = "09927b3e-c3b6-4075-a739-93ff555ca0c0";
                            userprofile.EmpID = "1f956egc-a476-4853-b382-7840eb4165f6";
                        }
                        catch
                        {
                            //HttpApplication application = HttpContext.Current.Application (HttpApplication)sender; 
                            HttpContext.Current.Response.Status = "401 OK";// "401 Unauthorized";
                            HttpContext.Current.Response.StatusCode = 401;
                            //HttpContext.Current.Response.AddHeader("WWW-Authenticate", "Basic");
                            HttpContext.Current.ApplicationInstance.CompleteRequest();

                        }
                    }
                }
                else
                {
                    return (UserProfile)System.Web.HttpContext.Current.Session[SessionKeys.CurrentUser];
                }
                System.Web.HttpContext.Current.Session[SessionKeys.CurrentUser] = userprofile;
                return userprofile;
            }
            set
            {
                System.Web.HttpContext.Current.Session[SessionKeys.CurrentUser] = value;
            }

        }


        /// <summary>
        /// 返回当前用户的profile
        /// </summary>
        public UserProfile CurrentUser
        {
            get
            {
                if ((null == System.Web.HttpContext.Current) || (null == System.Web.HttpContext.Current.Session) || (null == System.Web.HttpContext.Current.Session[SessionKeys.CurrentUser]))
                {
                    if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated == false)
                    {
                        System.Web.HttpContext.Current.Response.Redirect("~/Login.aspx?ReturnUrl=" + System.Web.HttpContext.Current.Request.Url);
                        return null;
                    }
                    else
                    {
                        string userLoginName = System.Web.HttpContext.Current.User.Identity.Name;
                        SecurityManager sm = new SecurityManager(DataContext);
                        UserProfile profile = sm.GetUserProfile(userLoginName);
                        if (profile.HaveMutilRole || profile.HaveMutilPosition)
                        {
                            System.Web.HttpContext.Current.Response.Redirect("~/Login.aspx?ReturnUrl=" + System.Web.HttpContext.Current.Request.Url);
                        }
                        System.Web.HttpContext.Current.Session[SessionKeys.CurrentUser] = profile;
                        return profile;
                    }
                }
                else
                {
                    return (UserProfile)System.Web.HttpContext.Current.Session[SessionKeys.CurrentUser];
                }
            }

            set
            {
                if ((null == System.Web.HttpContext.Current) || (null == System.Web.HttpContext.Current.Session))
                {
                    throw new BusinessException("无法获得当前用户的Session对象");
                }
                else
                {
                    System.Web.HttpContext.Current.Session[SessionKeys.CurrentUser] = value;
                }
            }
        }

        /// <summary>
        /// 返回最后的出错信息
        /// </summary>
        public string LastErrotMessage
        {
            get
            {
                if ((null == System.Web.HttpContext.Current) || (null == System.Web.HttpContext.Current.Session) || (null == System.Web.HttpContext.Current.Session[SessionKeys.LastErrotMessage]))
                {
                    return string.Empty;
                }
                else
                {
                    return (string)System.Web.HttpContext.Current.Session[SessionKeys.LastErrotMessage];
                }
            }

            set
            {
                if ((null == System.Web.HttpContext.Current) || (null == System.Web.HttpContext.Current.Session))
                {
                    throw new BusinessException("无法获得当前用户的Session对象");
                }
                else
                {
                    System.Web.HttpContext.Current.Session[SessionKeys.LastErrotMessage] = value;
                }
            }
        }
    }
}
